package mapReduce.demo05_shuffle.demo02_phoneTrafficSort;


import mapReduce.writable.PhoneTrafficWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;


public class PhoneTrafficSortComparator extends WritableComparator {

    public PhoneTrafficSortComparator() {
        super(PhoneTrafficWritable.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        PhoneTrafficWritable bean1 = (PhoneTrafficWritable) a;
        PhoneTrafficWritable bean2 = (PhoneTrafficWritable) b;

        int thisSumTraffic = bean1.getSumTraffic();
        int thatSumTraffic = bean2.getSumTraffic();
        int thisDownTraffic = bean1.getDownTraffic();
        int thatDownTraffic = bean2.getDownTraffic();

        if(thisSumTraffic==thatSumTraffic){
            return thatDownTraffic-thisDownTraffic;
        }else{
            return thisSumTraffic-thatSumTraffic;
        }
    }
}
